<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace plugin\friend_link\admin\controller;
use temmoku\controller;
use temmoku\db;
use temmoku\lib\pages;
use plugin\friend_link\public_class\cache;
class friend extends controller{
	
	public function index(){
		!isset($_GET['page']) && $_GET['page']=1;
		$rows='20';
		$WHERE="1";
		$data=db::select('*')->from(P_jab."friend")->orderByDESC(['reorder'])->setPaging($rows)->page($_GET['page'])->query();
		$this->assign('data', $data);
		$Get_Page=(new pages)->Pages(P_jab."friend",$WHERE,ADMIN_PLUGIN_URL."friend/index/{$url}",$rows,$_GET['page']);
		$this->assign('Get_Page', $Get_Page);
		$this->display(tpl('friend/index'));
	}
	public function add(){
		
		if('post'===$_POST['step']){
			if(!$_POST['title']){
				$this->err('链接名称不可以为空');
			}
			if(!$_POST['link']){
				$this->err('链接地址不可以为空');
			}
			
			if($_POST['cid']){
				$data=db::select('*')->from(P_jab."category")->where('cid='.$_POST['cid'])->row();
				$name=$data['name'];
			}else{
				$name='默认分类';
			}
			if($_POST['thumb'] && !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $_POST['thumb'] )){
				$_POST['thumb']=UploadFiles_MOVE($_POST['thumb'],'Friendlink');
			}
			$arr=[
				'title'=>$_POST['title'],
				'link'=>$_POST['link'],
				'thumb'=>$_POST['thumb'],
				'cid'=>$_POST['cid'],
				'vld'=>(int)strtotime($_POST['vld']),
				'is_w'=>(int)$_POST['is_w'],
				'state'=>(int)$_POST['state'],
				'name'=>$name,
				'reorder'=>(int)$_POST['reorder']
			];
			db::insert(P_jab."friend")->cols($arr)->query();
			cache::friend_cache();
			$this->success('添加成功');
		}
		$cdata=db::select('*')->from(P_jab."category")->orderByDESC(['reorder'])->query();
		$this->assign('cdata', $cdata);
		$this->display(tpl('friend/add'));
	}
	
	public function edit(){
		
		if('post'===$_POST['step']){
			$data=db::select('*')->from(P_jab."friend")->where('id='.$_POST['id'])->row();
			if(!$_POST['id']){
				$this->err('缺少必要参数');
			}
			if(!$_POST['title']){
				$this->err('链接名称不可以为空');
			}
			if(!$_POST['link']){
				$this->err('链接地址不可以为空');
			}
			
			if($_POST['cid']){
				$data=db::select('*')->from(P_jab."category")->where('cid='.$_POST['cid'])->row();
				$name=$data['name'];
			}else{
				$name='默认分类';
			}
			if($_POST['thumb'] && !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $_POST['thumb'] )){
				if($data['thumb'] != $_POST['thumb']){
					if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
						$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    				//删除备份文件
						del_article_images($oldthumb);
	    				@unlink($oldthumb);
					}
					$_POST['thumb']=UploadFiles_MOVE($_POST['thumb'],'Friendlink');
				}
			}else{
				if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
					$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    			//删除备份文件
					del_article_images($oldthumb);
	    			@unlink($oldthumb);
				}
			}
			$arr=[
				'title'=>$_POST['title'],
				'link'=>$_POST['link'],
				'thumb'=>$_POST['thumb'],
				'cid'=>$_POST['cid'],
				'vld'=>(int)strtotime($_POST['vld']),
				'is_w'=>(int)$_POST['is_w'],
				'state'=>(int)$_POST['state'],
				'name'=>$name,
				'reorder'=>(int)$_POST['reorder']
			];
			db::update(P_jab."friend")->cols($arr)->where('id='.$_POST['id'])->query();
			cache::friend_cache();
			$this->success('修改成功');
		}
		$cdata=db::select('*')->from(P_jab."category")->orderByDESC(['reorder'])->query();
		$this->assign('cdata', $cdata);
		$data=db::select('*')->from(P_jab."friend")->where('id='.$_GET['id'])->row();
		$this->assign('data', $data);
		$this->display(tpl('friend/add'));
	}
	public function del(){
		if(!$_GET['id']){
			$this->err('缺少必要参数');
		}
		$data=db::select('*')->from(P_jab."friend")->where('id='.$_GET['id'])->row();
	
		if($data['thumb']&& !preg_match('/(http|https):\/\/([\w\d\-_]+[\.\w\d\-_]+)[:\d+]?([\/]?[\w\/\.]+)/', $data['thumb'] ) ){
			$oldthumb=Temmoku_PATH.C('UPFILES_CATALOG').'/'.$data['thumb'];
	    	//删除备份文件
			del_article_images($oldthumb);
	    	@unlink($oldthumb);
		}
		db::delete(P_jab."friend")->where('id='.$_GET['id'])->query();
		cache::friend_cache();
		$this->success('删除成功');
	}
	
}
?>
